<?php
//+-----------------------------------------------------------
//| 人生是荒芜的旅行，冷暖自知，苦乐在心
//+-----------------------------------------------------------
//| Author:Janmas <janmas@126.com>
//+-----------------------------------------------------------
//| 
//+-----------------------------------------------------------

namespace app\admin\controller;

use app\admin\service\AdminService;
use app\admin\service\Auth;
use think\Exception;

/**
 * admin模块基类
 * @package app\admin\controller
 * @property Auth $auth
 * @property AdminService $server
 */
class Admin extends \app\BaseController
{
    protected $auth;

    protected $noNeedLogin =[];

    protected $noNeedAuth = ['*'];

    /**
     * service 的实例
     * @var AdminService
     */
    protected $service = null;

    public function initialize ()
    {
        $this->auth = Auth::instance();
        /**
         * 检测当前方法是否不需要登录
         */
        if(!$this->auth->noNeedLogin($this->request,$this->noNeedLogin)){
        	if(!$this->auth->isLogin()){
		        return $this->error('请登录先',url('Login/login'),1);
	        }
        	if(!$this->auth->noNeedAuth($this->request,$this->noNeedAuth)){
		        if(!$this->auth->auth($this->request->url())){
			        return $this->error('权限不足',url('index/index'),1);
		        }
	        }
        }
        $this->initServiceInstance();

        if($this->request->isGet() && !$this->request->isAjax() && strtolower($this->request->action()) == 'index'){
	        $this->assign('menu_buttons',$this->assignButton());
        }
        parent::initialize();
    }

    protected function assignButton(){
        //TODO:待优化
	    $menus = [];
	    $menu = null;
	    $menuModel = new \app\admin\model\Menu();
	    if(session('SYSTEM_ID') == 1 || $this->auth->noNeedAuth($this->request, $this->noNeedAuth)){
		    $menu = $menuModel->where('pid','IN',function ($query) {
			   $query->table('system_menu')->field('id')->where('controller',$this->request->controller())->where('action',$this->request->action());
		    })
			    ->where('type',2)
			    ->field('name,icon,link,action')
			    ->order('sort asc')
			    ->select();
	    }else{
		    $menu = \app\admin\model\Menu::alias('m')
			    ->leftJoin('system_permession p','p.permession_id = m.id')
			    ->where('m.pid',$this->auth->currentMenuId)
			    ->where('m.type',2)
			    ->where('p.role_id','in',array_column(session('userModel')->roles->toArray(),'id'))
			    ->field('m.name,m.icon,m.link,m.action')
			    ->select();
	    }

	    if(!is_null($menu) && !$menu->isEmpty()){
	    	$menu = $menu->toArray();
		    foreach($menu as $value){
		    	$menus[$value['action']] = $value;
		    }
	    }
	    return $menus;
    }

    //实例化Service层
    protected function initServiceInstance(){
        $controllerName = $this->request->controller();
        $serviceName = '\\app\\admin\\service\\'.$controllerName;
        if(class_exists($serviceName) && is_null($this->service)){
            $this->service = new $serviceName($this->request);
        }
    }
}